package com.jd.fruit.model.admin.auth;

import java.util.List;

import com.jd.fruit.common.annotation.Table;
import com.jd.fruit.common.model.BaseModel;
import com.jd.fruit.common.util.StringUtil;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

/**
 * 角色
 * 
 */
@SuppressWarnings("serial")
@Table("t_role_menu")
public class RoleMenu extends BaseModel<RoleMenu> { 
	public static final RoleMenu dao = new RoleMenu();
	
	
	/**
	 * 保存角色菜单
	 * @param roleMenu
	 */
	public void save(RoleMenu roleMenu,String roleMenus){
		try {
			String roleid=roleMenu.getStr("role_id");
			String[] arry = roleMenus.split(",");
			this.delRloeRelationMenu(roleid);
			for (int i = 0; i < arry.length; i++) {
				roleMenu.set("id", StringUtil.getKey());
				roleMenu.set("menu_id", arry[i]);
				roleMenu.save();
			}
		} catch (Exception e) {
			throw new RuntimeException("角色菜单保存异常！", e);
		}
	}
	/**
	 * 删除与角色相关联菜单的数据
	 * @param roleid
	 */
	public void delRloeRelationMenu(String roleid) {
		Db.deleteById("t_role_menu", "role_id", roleid);
	}

	
	/**
	 * 获得当前角色包含的菜单
	 * @param detailId
	 * @return
	 */
	public String roleMap(String detailId) {
		String mapStr="";
		if(StrKit.notBlank(detailId)){
			String sql = "select * from t_role_menu where role_id=?";
			List<Record>  roleresult = Db.find(sql,detailId);
			for (Record record : roleresult) {
				mapStr += record.getStr("menu_id");
			}
		}
		return mapStr;
	}


	@Override
	public void initWhere() {
		// TODO Auto-generated method stub
		
	}
}
